import * as vscode from 'vscode';
import { TaskStatus, getStatusText } from '../models/todo';
import { TodoProvider } from './todo-provider';
import { BaseTreeItem } from '../models/base-tree-item';

class StatsItem extends BaseTreeItem {
    constructor(label: string, count: number) {
        super(label, vscode.TreeItemCollapsibleState.None);
        this.setDescription(count.toString());
    }
}

export class StatsProvider implements vscode.TreeDataProvider<StatsItem> {
    private _onDidChangeTreeData: vscode.EventEmitter<StatsItem | undefined | null | void> = new vscode.EventEmitter<StatsItem | undefined | null | void>();
    readonly onDidChangeTreeData: vscode.Event<StatsItem | undefined | null | void> = this._onDidChangeTreeData.event;

    constructor(private todoProvider: TodoProvider) {}

    refresh(): void {
        this._onDidChangeTreeData.fire();
    }

    getTreeItem(element: StatsItem): vscode.TreeItem {
        return element;
    }

    getChildren(element?: StatsItem): Thenable<StatsItem[]> {
        if (!element) {
            const stats = this.todoProvider.getTasksStats();
            const items: StatsItem[] = [
                new StatsItem('总任务数', stats.total),
                new StatsItem(`${getStatusText(TaskStatus.Pending)}`, stats.pending),
                new StatsItem(`${getStatusText(TaskStatus.InProgress)}`, stats.inProgress),
                new StatsItem(`${getStatusText(TaskStatus.Done)}`, stats.done),
                new StatsItem(`${getStatusText(TaskStatus.Cancelled)}`, stats.cancelled)
            ];
            return Promise.resolve(items);
        }
        return Promise.resolve([]);
    }
}